﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Timer = System.Timers.Timer;

namespace Smolder
{
    public class FolderSynchronizerManager
    {
        public double TaskInterval { get; set; }
        public FolderSynchronizer FolderSynchronizer { get; set; }

        public FolderSynchronizerManager()
        {
            TaskInterval = 10 * 60 * 1000;//10 minutes
            FolderSynchronizer = new FolderSynchronizer();
        }

        public void Start()
        {
            Timer t = new Timer(TaskInterval);
            t.AutoReset = true;
            t.Elapsed += (s, e) =>
            {
                if (_executing) return;
                _executing = true;
                Thread syncThread = new Thread(Execute);
                syncThread.Priority = ThreadPriority.Lowest;
                syncThread.Start();
            };
        }

        private void Execute(object obj)
        {
            try
            {
                FolderSynchronizer.Execute();
            }
            finally
            {
                _executing = false;
            }
        }

        public void Stop()
        {
        }
        private bool _executing = false;
    }
}
